package top.psjj.mall.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.psjj.mall.comment.Constant;
import top.psjj.mall.exception.MallException;
import top.psjj.mall.exception.MallExceptionEnum;
import top.psjj.mall.mapper.CartMapper;
import top.psjj.mall.mapper.ProductMapper;
import top.psjj.mall.model.po.Cart;
import top.psjj.mall.model.po.CartExample;
import top.psjj.mall.model.po.Product;
import top.psjj.mall.model.vo.CartVo;
import top.psjj.mall.service.api.CartService;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<CartVo> updateCart(Integer userid, Integer productId, Integer count) {
        //校验商品是否合法
        validProduct(productId,count);
        Cart cart = cartMapper.selectByProductIdAndUserId(productId,userid);
        if(cart==null){
            throw new MallException(MallExceptionEnum.UPDATE_FAILED);
        }
        cart.setQuantity(count);
        cart.setSelected(Constant.CartSelected.SELECTED);
        int c = cartMapper.updateByPrimaryKeySelective(cart);
        if(c==0){
            throw new MallException(MallExceptionEnum.UPDATE_FAILED);
        }
        return list(userid);
    }

    @Override
    public List<CartVo> delectCart(Integer userId, Integer productId) {
        Cart cart = cartMapper.selectByProductIdAndUserId(productId,userId);
        if(cart==null){
            throw new MallException(MallExceptionEnum.DELETE_FAILED);
        }
        CartExample example = new CartExample();
        example.createCriteria().andUserIdEqualTo(userId).andProductIdEqualTo(productId);
        cartMapper.deleteByExample(example);
        return list(userId);
    }

    @Override
    public List<CartVo> select(Integer id, Integer productId, Integer selected) {
        Cart cart = cartMapper.selectByProductIdAndUserId(productId,id);
        if(cart==null){
            throw new MallException(MallExceptionEnum.NOT_EXISTS);
        }
        cartMapper.updateSelectStatus(id,productId,selected);
        return list(id);
    }

    @Override
    public List<CartVo> selectAll(Integer id, Integer selected) {
        cartMapper.updateSelectStatus(id,null,selected);
        return list(id);
    }

    @Override
    public List<CartVo> addCart(Integer userId, Integer productId, Integer count) {
        //校验商品是否合法
        validProduct(productId,count);
        //通过userid查询购物车有没有商品id没有就添加，有就修改数量
        Cart cart = cartMapper.selectByProductIdAndUserId(productId,userId);
        if (cart==null){
            Cart cart1 = new Cart();
            cart1.setProductId(productId);
            cart1.setUserId(userId);
            cart1.setQuantity(count);
            cart1.setSelected(Constant.CartSelected.SELECTED);
            cartMapper.insertSelective(cart1);
        }else {
            cart.setQuantity(cart.getQuantity()+count);
            cartMapper.updateByPrimaryKeySelective(cart);
        }
        //返回购物车列表
        return list(userId);
    }

    public List<CartVo> list(Integer userId) {
        List<CartVo> list = cartMapper.selectByUserId(userId);
        for (CartVo cartVo : list){
            cartVo.setTotalPrice(cartVo.getPrice()*cartVo.getQuantity());
        }
        return list;
    }

    //校验商品是否合法
    private void validProduct(Integer productId, Integer count) {
        Product product = productMapper.selectByPrimaryKey(productId);
        if(product==null || product.getStatus()==Constant.SellStatus.NOT_SELL){
            throw new MallException(MallExceptionEnum.NOT_SELL);
        }
        if(product.getStock()<count){
            throw new MallException(MallExceptionEnum.NEED_STOCK);
        }
    }
}
